/*============================================================================
 * Project    : GameZone
 * Name       : Image.cpp
 * Author     : Administrator
 * Version    : 1.0
 * Copyright  : Copyright (c) 2009,Aspire-tech technology com, Ltd.
 * All rights reserved.
 * Created on : 2010-9-27 ����11:17:22
 * Description: {description}
 *============================================================================
 */

#include "Image.h"
#include "ImageConverterEngine.h"

CImage::CImage()
{
    iBitmap = NULL;
    iMaskBitmap = NULL;
}

CImage::~CImage()
{
    delete iBitmap;
    iBitmap = NULL;
    delete iMaskBitmap;
    iMaskBitmap = NULL;
}

void CImage::setBitmap(CFbsBitmap* aBitmap)
{
    delete iBitmap;
    iBitmap = NULL; 
    iBitmap = aBitmap;
}

CFbsBitmap* CImage::Bitmap(TBool aCopy)
{
    if (iBitmap)
    {
        if (!aCopy)
            return iBitmap;
        else
        {
            CFbsBitmap* bitmap = new (ELeave) CFbsBitmap();
            User::LeaveIfError(bitmap->Duplicate(iBitmap->Handle()));
            return bitmap;
        }
    }
    else
        return NULL;
}

void CImage::setMaskBitmap(CFbsBitmap* aBitmap)
{
    delete iMaskBitmap;
    iMaskBitmap = NULL;
    iMaskBitmap = aBitmap;
}

CFbsBitmap* CImage::MaskBitmap(TBool aCopy)
{
    if (iMaskBitmap)
    {
        if (!aCopy)
            return iMaskBitmap;
        else
        {
            CFbsBitmap* bitmap = new (ELeave) CFbsBitmap();
            User::LeaveIfError(bitmap->Duplicate(iMaskBitmap->Handle()));
            return bitmap;
        }
    }
    else
        return NULL;
}

void CImage::ReadImageFromFileL(const TDesC& aFile)
{
    TFileName fileName;
    fileName.Append(aFile);
    CImageConverterEngine* ImageConverter = CImageConverterEngine::NewL(NULL);
    ImageConverter->StartToDecodeAsyL(fileName);
    if (ImageConverter->GetBitmap())
    {
        delete iBitmap;
        iBitmap = NULL;
        iBitmap = new (ELeave) CFbsBitmap();
        iBitmap->Duplicate(ImageConverter->GetBitmap()->Handle());
    }
    if (ImageConverter->GetMaskBitmap())
    {
        delete iMaskBitmap;
        iMaskBitmap = NULL;
        iMaskBitmap = new (ELeave) CFbsBitmap();
        iMaskBitmap->Duplicate(ImageConverter->GetMaskBitmap()->Handle());
    }
    delete ImageConverter;
    ImageConverter = NULL;
}

void CImage::ReadImageFromContentL(const TDesC8& aContent)
{
    CImageConverterEngine* ImageConverter = CImageConverterEngine::NewL(NULL);
    ImageConverter->StartToDecodeAsyL(aContent);
    //TODO 5th
    ImageConverter->ScaleAsy(150);
    if (ImageConverter->GetBitmap())
    {
        delete iBitmap;
        iBitmap = NULL;
        iBitmap = new (ELeave) CFbsBitmap();
        iBitmap->Duplicate(ImageConverter->GetBitmap()->Handle());
    }
    if (ImageConverter->GetMaskBitmap())
    {
        delete iMaskBitmap;
        iMaskBitmap = NULL;
        iMaskBitmap = new (ELeave) CFbsBitmap();
        iMaskBitmap->Duplicate(ImageConverter->GetMaskBitmap()->Handle());
    }
    delete ImageConverter;
    ImageConverter = NULL;
}

TSize CImage::SizeInPixels()
{
    TSize sz(0,0);
    if (iBitmap)
        sz = iBitmap->SizeInPixels();
    return sz;
}
